﻿using System;
using Spring.Scheduling.Quartz;
using DotNetServiceLib.Core;
using Common.Logging;
using System.Data.SqlClient;
using System.Data;

namespace DotNetServiceLib.Core
{
    /// <summary>
    /// Allows to execute a parameterless stored procedure from a Microsot SQL Server database.
    /// Beware that the connection string is stored as a parameter inside the spring-objects.xml file. This
    /// may not be suited for all scenarios since it can be the cause of serious security issues. 
    /// </summary>
    public class TaskMSSQLStoredProcedure : QuartzJobObject, ITask
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private string name = "";
        private string sqlConnectionString = "";
        private string storedProcedureName = "";
        private string target = "";
        private string className = "";
        private string methodName = "";
        private string arguments = "";
        private bool loggingIsEnabled = true;
        
        protected override void ExecuteInternal(Quartz.JobExecutionContext context)
        {
            using (SqlConnection connection = new SqlConnection(sqlConnectionString))
            {
                SqlCommand oCom = connection.CreateCommand();
                oCom.CommandType = CommandType.StoredProcedure;
                oCom.CommandText = storedProcedureName;
                oCom.Connection = connection;

                try
                {
                    connection.Open();
                    log.Info(String.Format("Invoking stored procedure '{0}'.", storedProcedureName));
                    oCom.ExecuteNonQuery();
                }
                catch (SqlException sqlExc)
                {
                    log.Error(String.Format("Invoking stored procedure '{0}' throwed error {1}.", storedProcedureName, sqlExc.ToString()));
                }
                catch (Exception exc)
                {
                    log.Error(String.Format("Invoking stored procedure '{0}' throwed error {1}.", storedProcedureName, exc.ToString()));
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        #region Miembros de ITask
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public string Target
        {
            get
            {
                return target;
            }
            set
            {
                target = value;
            }
        }
        public string Arguments
        {
            get
            {
                return arguments;
            }
            set
            {
                arguments = value;
            }
        }
        public bool LoggingIsEnabled
        {
            get
            {
                return loggingIsEnabled;
            }
            set
            {
                loggingIsEnabled = value;
            }
        }
        public string ClassName
        {
            get
            {
                return className;
            }
            set
            {
                className = value;
            }
        }
        public string MethodName
        {
            get
            {
                return methodName;
            }
            set
            {
                methodName = value;
            }
        }
        public string SqlConnectionString
        {
            get
            {
                return sqlConnectionString;
            }
            set
            {
                sqlConnectionString = value;
            }
        }
        public string StoredProcedureName
        {
            get
            {
                return storedProcedureName;
            }
            set
            {
                storedProcedureName = value;
            }
        }
        #endregion
    }
}
